<template>
  <a-form-model layout="vertical" ref="ruleForm" :rules="rules" :model="model">
    <z-form-item :datas="controlPersonType == 1 ? fieldArr1 : fieldArr2" :rules="rules" :form="model" :gutter="72"
      @change="getChange" @rows="onRows">
      <!--//自定义头像上传-->
      <template slot="slots-avatar">
        <j-image-upload bizPath="scott/pic" v-model="model.avatar" />
      </template>
      <!--//自定义身份证号-->
      <template slot="slots-identityCard">
        <a-space>
          <a-input v-model="model.identityCard" placeholder="请输入身份证号" style="width: 160px;" />
          <a-button @click="onVerify">获取验证</a-button>
        </a-space>
      </template>
      <!--//自定义接受戒毒方式及起止时间-->
      <template slot="slots-drugKeyword">
        <!-- {{model.drugKeyword}} -->
        <a-checkbox-group v-model="model.drugKeyword" @change="onChange" class="slots-checkbox">
          <template v-for="(item, index) in optionsWithDisabled">
            <a-checkbox :value="item.value" @change="onChangeCheck" :key="index">
              <span class="label">{{ item.label }}</span>
              <a-range-picker v-if="item.value == 1" v-model="model.date1" />
              <a-range-picker v-if="item.value == 2" v-model="model.date2" />
              <a-range-picker v-if="item.value == 3" v-model="model.date3" />
              <span v-if="item.value == 4" style="display: inline-flex;">
                <a-range-picker v-model="model.date4" />
                <j-dict-select-tag v-model="model.prisonsType" dictCode="prisons_type" placeholder="请选择监所类型"
                  style="margin-left:10px;" />
                  <a-input v-model="model.prisonsName" placeholder="请输入监所" style="width: 160px;margin-left:10px;" />
                <!-- <a-tree-select style="width:100%;margin-left:10px;" :multiple="false" allowClear
                  :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }" :treeData="treeData"
                  v-model="model.prisonsName" @change="onChange1" placeholder="请选择监所">
                </a-tree-select> -->
              </span>
            </a-checkbox>
          </template>
        </a-checkbox-group>
      </template>
    </z-form-item>
  </a-form-model>
</template>

<script>
import { queryDepartByArea, queryDepartTreeList } from '@/api/api'
import { getOutsideByIdentityCard } from '@/api/sjsk/personService'
var phoneValidator = (rule, value, callback) => {
  if (!value || new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(value)) {
    callback()
  } else {
    callback('请输入正确的联系方式')
  }
}
var identityCardValidator = (rule, value, callback) => {
  if (!value || new RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/).test(value)) {
    // callback('验证成功，身份证号码正确')
    callback()
  } else {
    callback('请输入正确的身份证号码')
  }
}
export default {
  props: ['current', 'controlPersonType', 'value'],
  data() {
    return {
      model: {},
      fieldArr1: [
        { label: '头像上传', prop: 'avatar' },
        { label: '姓名', prop: 'realName', tagType: 'input', span: 6 },
        { label: '社戒社康类型', prop: 'sjskType', tagType: 'dict-select', dictCode: 'sjsk_type', span: 6 },
        { label: '社戒社康开始日期', prop: 'sjskStartDate', tagType: 'date-picker', format: 'YYYY-MM-DD', span: 6 },
        { label: '社戒社康结束日期', prop: 'sjskEndDate', tagType: 'date-picker', format: 'YYYY-MM-DD', span: 6 },
        { label: '别名/绰号', prop: 'nickName', tagType: 'input', span: 6 },
        { label: '性别', prop: 'sex', tagType: 'dict-select', dictCode: 'sex', span: 6 },
        { label: '出生年月', prop: 'birthday', tagType: 'date-picker', format: 'YYYY-MM-DD', span: 6 },
        { label: '文化程度', prop: 'education', tagType: 'dict-select', dictCode: 'degreeEducation', span: 6 },
        { label: '身高（cm）', prop: 'height', tagType: 'input', span: 6 },
        { label: '民族', prop: 'nation', tagType: 'dict-select', dictCode: 'nation', span: 6 },
        { label: '身份证号', prop: 'identityCard', span: 6 },
        { label: '联系电话', prop: 'phone', tagType: 'input', span: 6 },
        { label: '其他证件类型', prop: 'otherCertificateType', tagType: 'dict-select', dictCode: 'other_certificate_type', span: 6 },
        { label: '其他证件号码', prop: 'otherCertificateNumber', tagType: 'input', span: 6 },
        { label: '工作单位', prop: 'workUnit', tagType: 'input', span: 6 },
        { label: '单位电话', prop: 'unitPhone', tagType: 'input', span: 6 },
        { label: '户籍地地区', prop: 'houseLevels', tagType: 'linkage-area', span: 12 },
        { label: '户籍地详址', prop: 'householdAddress', tagType: 'input', span: 6 },
        { label: '户籍地派出所', prop: 'householdPoliceStation', tagType: 'input', span: 6 },
        { label: '居住地地区', prop: 'currentLevels', tagType: 'linkage-area', span: 12 },
        { label: '居住地详址', prop: 'currentAddress', tagType: 'input', span: 6 },
        { label: '居住地派出所', prop: 'currentPoliceStation', tagType: 'input', span: 6, disabled: true },
        { label: '婚姻状况', prop: 'marriage', tagType: 'dict-select', dictCode: 'marriage', span: 6 },
        { label: '从业状况', prop: 'workSituation', tagType: 'dict-select', dictCode: 'work_situation', span: 6 },
        { label: '健康状况（是否有疾病）', prop: 'healthStatus', tagType: 'dict-select', dictCode: 'dict_yn', span: 6 },
        { label: '疾病类型', prop: 'diseaseType', tagType: 'input', span: 6 },
        { label: '戒毒期间是否参加药物维持治疗', prop: 'isJoinMedicineTreatment', tagType: 'dict-select', dictCode: 'dict_yn', span: 6 },
        { label: '戒毒期间是否进入康复场所', prop: 'isIntoRecurePlace', tagType: 'dict-select', dictCode: 'dict_yn', span: 6 },
        { label: '备注', prop: 'remark', tagType: 'input', span: 6 },
        { label: '初次吸毒日期', prop: 'firstDrugDate', tagType: 'date-picker', format: 'YYYY-MM-DD', span: 6 },
        { label: '风险类别', prop: 'riskCategory', tagType: 'dict-select', dictCode: 'risk_category', span: 6 },
        { label: '管控等级', prop: 'controlLevel', tagType: 'dict-select', dictCode: 'control_level', span: 6 },
        { label: '出所日期', prop: 'outDate', tagType: 'date-picker', format: 'YYYY-MM-DD', span: 6 },
        { label: '户籍地专干姓名', prop: 'registerManagerName', tagType: 'input', span: 6 },
        { label: '专干联系方式', prop: 'registerManagerPhone', tagType: 'input', span: 6 },
        { label: '户籍地民警姓名', prop: 'policeName', tagType: 'input', span: 6 },
        { label: '民警联系方式', prop: 'policePhone', tagType: 'input', span: 6 },
        { label: '滥用毒品方式', prop: 'drugWays', tagType: 'dict-multi-tag', dictCode: 'drug_ways', type: 'checkbox' },
        { label: '滥用毒品种类', prop: 'drugType', tagType: 'dict-multi-tag', dictCode: 'drug_type', type: 'checkbox' },
      ],
      fieldArr2: [
        { label: '头像上传', prop: 'avatar' },
        { label: '姓名', prop: 'realName', tagType: 'input', span: 6 },
        { label: '别名/绰号', prop: 'nickName', tagType: 'input', span: 6 },
        { label: '性别', prop: 'sex', tagType: 'dict-select', dictCode: 'sex', span: 6 },
        { label: '出生年月', prop: 'birthday', tagType: 'date-picker', format: 'YYYY-MM-DD', span: 6 },
        { label: '身高（cm）', prop: 'height', tagType: 'input', span: 6 },
        { label: '风险类别', prop: 'riskCategory', tagType: 'dict-select', dictCode: 'risk_category', span: 6 },
        { label: '联系电话', prop: 'phone', tagType: 'input', span: 6 },
        { label: '身份证号', prop: 'identityCard', span: 6 },
        { label: '人员类型', prop: 'personType', tagType: 'dict-select', dictCode: 'personType', span: 6 },
        { label: '文化程度', prop: 'education', tagType: 'dict-select', dictCode: 'degreeEducation', span: 6 },
        { label: '其他证件类型', prop: 'otherCertificateType', tagType: 'dict-select', dictCode: 'other_certificate_type', span: 6 },
        { label: '其他证件号码', prop: 'otherCertificateNumber', tagType: 'input', span: 6 },
        { label: '婚姻状况', prop: 'marriage', tagType: 'dict-select', dictCode: 'marriage', span: 6 },
        { label: '民族', prop: 'nation', tagType: 'dict-select', dictCode: 'nation', span: 6 },
        { label: '国籍', prop: 'country', tagType: 'dict-select', dictCode: 'country', span: 6 },
        { label: '从业状况', prop: 'workSituation', tagType: 'dict-select', dictCode: 'work_situation', span: 6 },
        { label: '户籍地地区', prop: 'houseLevels', tagType: 'linkage-area', span: 12 },
        { label: '户籍地详址', prop: 'householdAddress', tagType: 'input', span: 6 },
        { label: '户籍地派出所', prop: 'householdPoliceStation', tagType: 'input', span: 6 },
        { label: '居住地地区', prop: 'currentLevels', tagType: 'linkage-area', span: 12 },
        { label: '居住地详址', prop: 'currentAddress', tagType: 'input', span: 6 },
        { label: '居住地派出所', prop: 'currentPoliceStation', tagType: 'input', disabled: true, span: 6 },
        { label: '工作单位', prop: 'workUnit', tagType: 'input', span: 6 },
        { label: '单位电话', prop: 'unitPhone', tagType: 'input', span: 6 },
        { label: '初次吸毒日期', prop: 'firstDrugDate', tagType: 'date-picker', format: 'YYYY-MM-DD', span: 6 },
        { label: '既往戒毒次数', prop: 'abstinenceDrugTimes', tagType: 'input', span: 6 },
        { label: '是否复吸', prop: 'isResorption', tagType: 'dict-select', dictCode: 'dict_yn', span: 6 },
        { label: '管控等级', prop: 'controlLevel', tagType: 'dict-select', dictCode: 'control_level', span: 6 },
        { label: '户籍地专干姓名', prop: 'registerManagerName', tagType: 'input', span: 6 },
        { label: '专干联系方式', prop: 'registerManagerPhone', tagType: 'input', span: 6 },
        { label: '户籍地民警姓名', prop: 'policeName', tagType: 'input', span: 6 },
        { label: '民警联系方式', prop: 'policePhone', tagType: 'input', span: 6 },
        { label: '滥用毒品方式', prop: 'drugWays', tagType: 'dict-multi-tag', dictCode: 'drug_ways', type: 'checkbox' },
        { label: '滥用毒品种类', prop: 'drugType', tagType: 'dict-multi-tag', dictCode: 'drug_type', type: 'checkbox' },
        { label: '接受戒毒方式及起止时间', prop: 'drugKeyword' },
      ],
      optionsWithDisabled: [
        { label: '自愿戒毒', value: '1' },
        { label: '社区戒毒', value: '2' },
        { label: '社区康复', value: '3' },
        { label: '强制隔离戒毒', value: '4' },
        { label: '无', value: '5' },
      ],
      rules: {
        // 头像上传
        avatar: [{ required: true, message: '请上传头像', trigger: 'change' },],
        realName: [{ required: true, message: '请输入', trigger: 'change' },],
        sjskType: [{ required: true, message: '请选择', trigger: 'change' },],
        sjskStartDate: [{ required: true, message: '请选择', trigger: 'change' },],
        sjskEndDate: [{ required: true, message: '请选择', trigger: 'change' },],
        sex: [{ required: true, message: '请选择', trigger: 'change' },],
        birthday: [{ required: true, message: '请选择', trigger: 'change' },],
        education: [{ required: true, message: '请选择', trigger: 'change' },],
        height: [{ required: true, message: '请输入', trigger: 'change' },],
        nation: [{ required: true, message: '请选择', trigger: 'change' },],
        country: [{ required: true, message: '请选择', trigger: 'change' },],
        identityCard: [
          { required: true, message: '请输入', trigger: 'change' },
          { validator: identityCardValidator },
        ],
        personType: [{ required: true, message: '请选择', trigger: 'change' },],
        education: [{ required: true, message: '请选择', trigger: 'change' },],
        phone: [
          { required: true, message: '请输入', trigger: 'change' },
          { validator: phoneValidator },
        ],
        houseLevels: [{ required: true, message: '请选择', trigger: 'change' },],
        householdAddress: [{ required: true, message: '请输入', trigger: 'change' },],
        householdPoliceStation: [{ required: true, message: '请输入', trigger: 'change' },],
        currentLevels: [{ required: true, message: '请选择', trigger: 'change' },],
        currentAddress: [{ required: true, message: '请输入', trigger: 'change' },],
        // currentPoliceStation: [{ required: true, message: '请选择居住地地区', trigger: 'change' },],
        marriage: [{ required: true, message: '请选择', trigger: 'change' },],
        workSituation: [{ required: true, message: '请选择', trigger: 'change' },],
        healthStatus: [{ required: true, message: '请选择', trigger: 'change' },],
        isJoinMedicineTreatment: [{ required: true, message: '请选择', trigger: 'change' },],
        isIntoRecurePlace: [{ required: true, message: '请选择', trigger: 'change' },],
        firstDrugDate: [{ required: true, message: '请选择', trigger: 'change' },],
        riskCategory: [{ required: true, message: '请选择', trigger: 'change' },],
        isResorption: [{ required: true, message: '请选择', trigger: 'change' },],
        abstinenceDrugTimes: [{ required: true, message: '请输入', trigger: 'change' },],
        controlLevel: [{ required: true, message: '请选择', trigger: 'change' },],
        outDate: [{ required: true, message: '请选择', trigger: 'change' },],
        registerManagerName: [{ required: true, message: '请输入', trigger: 'change' },],
        registerManagerPhone: [
          { required: true, message: '请输入', trigger: 'change' },
          { validator: phoneValidator },
        ],
        policeName: [{ required: true, message: '请输入', trigger: 'change' },],
        policePhone: [
          { required: true, message: '请输入', trigger: 'change' },
          { validator: phoneValidator },
        ],
        drugWays: [{ required: true, message: '请选择', trigger: 'change' },],
        drugType: [{ required: true, message: '请选择', trigger: 'change' },],
        drugKeyword: [{ required: true, type: 'array', message: '请选择', trigger: 'change' },],
      },
      // treeData: [],
    }
  },
  created() {
    // this.loadDepart()
  },
  computed: {
    getValue() {
      let value = this.value ? this.value : {}
      return value
    }
  },
  watch: {
    getValue: {
      immediate: true,
      deep: true,
      handler(val) {
        console.log(val)
        this.model = Object.assign({}, this.model, val)
      }
    },
    // model:{
    //   // immediate: true,
    //   deep: true,
    //   handler(val) {
    //     this.$emit('change', this.current, val)
    //   }
    // }
  },
  methods: {
    //根据部门id获取所在的组织架构
    loadDepart() {
      queryDepartTreeList({ type: 3, child: 1, departType: 1 }).then(res => {
        if (res.success) {
          this.treeData = res.result[0].children
          // this.form.provinceCityAreaCode = res.result[0].adminDivisionStr //保存行政区划编码
          // let areaArr = res.result[0].adminDivisionStr.split(',')
          // this.$set(this.form, "adminDivisions", areaArr)
          // this.getAreaTittle(areaArr)
          // this.getDivisionAdmin(this.form.provinceCityAreaCode)
        }
      })
    },
    onChange() { },
    // onChange1(value, label) {
    //   // console.log(value, label)
    //   this.model.prisonsName = label[0]
    //   console.log(this.model.prisonsName)
    // },
    onChangeCheck() { },
    getChange(prop, value) {
      if (prop === 'houseLevels') {
        let lastData = value.length > 0 ? value[value.length - 1] : ''
        this.$set(this.model, 'houseLevel', value.toString())
        this.$set(this.model, 'houseDepartmentId', lastData)
      }
      if (prop === 'currentLevels') {
        // console.log("3333333333333")
        let lastData = value.length > 0 ? value[value.length - 1] : ''
        let flastData = value.length > 0 ? value[value.length - 2] : ''
        this.$set(this.model, 'currentLevel', value.toString())
        this.$set(this.model, 'currentDepartmentId', lastData)
        if (value && value.length === 5) this.getQueryDepartByArea(flastData)
        // console.log(this.model)
      }
    },
    onRows(prop, labels) {
      if (prop === 'currentLevels') {
        let label = labels ? labels.map(v => v.title) : ''
        this.$set(this.model, 'currentTown', label.toString()) //设置中文
      }
    },
    onVerify() {
      this.$refs['ruleForm'].validateField('identityCard', (error) => {
        if (!error) {
          // getOutsideByIdentityCard(this.model.identityCard)
          //   .then(res => {
          //     if (res.success) {
          //       const { result } = res
          //       if (!result) {
          //         this.$message.warn('该人员正在我省执行异地管控，请核实后再申请！')
          //         return
          //       }
          //     } else {
          //       this.$message.warn(res.message?res.message: '操作失败')
          //     }
          //   })
          //   .catch(e => {
          //     this.$message.warn('请求失败！')
          //   })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 通过执行社区筛选派出所信息
    getQueryDepartByArea(code) {
      queryDepartByArea({ type: 2, code })
        .then(res => {
          if (res.success) {
            const { result } = res
            if (result && Object.keys(result).length > 0 && result.departName) {
              let departName = result.departName
              this.$set(this.model, 'currentPoliceStation', departName)
            }
          } else {
            this.$message.warn(res.message)
          }
        })
        .catch(e => {
          this.$message.warn('请求失败！')
        })
    }
  }
}
</script>

<style scoped lang="less">
.slots-checkbox {
  :deep(&.ant-checkbox-group) {
    .ant-checkbox-wrapper {
      display: flex;
      align-items: center;
      margin-bottom: 16px;
      margin-left: 0;

      .label {
        width: 200px;
        display: inline-block;
      }

      .ant-calendar-picker {
        width: 240px;
        flex: 0 0 240px;
      }

      .ant-select {
        flex: 0 0 200px;
      }
    }
  }
}
</style>
